﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace EXAM
{
    public partial class DetailTest : System.Web.UI.Page
    {
        string sErr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserGuest"] == null)
            {
                Response.Redirect("Login.aspx");
                if (Request["TestNo"] != null)
                {
                    BUS.TestControl testCtr = new BUS.TestControl();
                    DTO.TestInfo testInf = testCtr.GetByNo(Convert.ToInt32(Request["TestNo"]), ref sErr);
                    lbName.Text = testInf.Name;
                }
            }
        }



        protected void dgvList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            if (Request["TestNo"] != null)
            {
                BUS.Test_LogControl logCtr = new BUS.Test_LogControl();
                DTO.UserInfo usrinf = (DTO.UserInfo)Session["UserGuest"];

                dgvList.DataSource = logCtr.Get_ByNo(Convert.ToInt32(Request["TestNo"]), ref sErr);
            }
        }

        protected void dgvList_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item != null)
            {
                if (e.Item is GridDataItem)
                {
                    Image img = (Image)e.Item.FindControl("img");
                    if (((DataRowView)e.Item.DataItem)["Score"] != null && ((DataRowView)e.Item.DataItem)["Score"] != DBNull.Value)
                    {
                        int score = Convert.ToInt32(((DataRowView)e.Item.DataItem)["Score"]);
                        if (score == 1)
                            img.ImageUrl = "images/correct.png";
                    }

                }
            }
        }

        protected void dgvList_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            if (e.Action == Telerik.Web.UI.GridGroupsChangingAction.Ungroup)
            {
                foreach (GridGroupByField group in e.Expression.SelectFields)
                {
                    GridColumn col = dgvList.Columns.FindByDataField(group.FieldName);
                    if (col.Visible == false)
                    {
                        col.Visible = true;
                    }
                }
            }
            else if (e.Action == Telerik.Web.UI.GridGroupsChangingAction.Group)
            {
                foreach (GridGroupByField group in e.Expression.SelectFields)
                {
                    GridColumn col = dgvList.Columns.FindByDataField(group.FieldName);
                    if (col.Visible == true)
                    {
                        col.Visible = false;
                    }
                }
            }
        }

        protected void btnLearn_Click(object sender, EventArgs e)
        {
            if (Request["TestNo"] != null)
            {
                Response.Redirect("Learning.aspx?TestNo=" + Request["TestNo"].ToString());
            }
        }
    }
}
